﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Sys;
using System.Data.OracleClient;
using System.Data;

namespace CardLogistics.DAL.Sys
{
    public class Sys_SysUserInfoData
    {
        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Sys_SysUserInfo model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,100),
					new OracleParameter("vUSERNAME", OracleType.VarChar,20),
					new OracleParameter("vUSERPWD", OracleType.VarChar,100),
					new OracleParameter("vUSERFULLNAME", OracleType.VarChar,20),
					new OracleParameter("vROLEID", OracleType.VarChar),
					new OracleParameter("vUSERSTATUS", OracleType.Number),
					new OracleParameter("vCREATEUSER", OracleType.VarChar),
					new OracleParameter("vREMARKS", OracleType.VarChar)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.USERNAME;
                parameters[2].Value = model.USERPWD;
                parameters[3].Value = model.USERFULLNAME;
                parameters[4].Value = model.ROLEID;
                parameters[5].Value = model.USERSTATUS;
                parameters[6].Value = model.CREATEUSER;
                parameters[7].Value = model.REMARKS;
                string StrOutPar = DbHelperSQL.RunProcedure("P_SYS_SYSUSERINFO_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>修改成功返回true、否则返回false</retu     rns>
        public string Update(Sys_SysUserInfo model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,100),
					new OracleParameter("vUSERNAME", OracleType.VarChar,20),
					new OracleParameter("vUSERPWD", OracleType.VarChar,50),
					new OracleParameter("vUSERFULLNAME", OracleType.VarChar,20),
					new OracleParameter("vROLEID", OracleType.VarChar,50),
					new OracleParameter("vUSERSTATUS", OracleType.Number),
					new OracleParameter("vREMARKS", OracleType.VarChar,500),
                    new OracleParameter("varOut", OracleType.VarChar,100)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.USERNAME;
                parameters[2].Value = model.USERPWD;
                parameters[3].Value = model.USERFULLNAME;
                parameters[4].Value = model.ROLEID;
                parameters[5].Value = model.USERSTATUS;
                parameters[6].Value = model.REMARKS;
                parameters[7].Direction = ParameterDirection.Output;
                return DbHelperSQL.RunProcedure("P_SYS_SYSUSERINFO_Update", parameters, parameters[7]).ToString();
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,50),
					    new OracleParameter("vID",  OracleType.VarChar)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_SYS_SYSUSERINFO_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "SYS_SYSUSERINFO", StrFields, "id", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_SysUserInfo GetModel(string userName, string userPwd)
        {
            DataTable dt = GetList(" USERNAME='" + userName + "' and USERPWD='" + userPwd + "' ", "*");
            if (dt == null || dt.Rows.Count == 0)
                return null;
            DataRow row = dt.Rows[0];
            Sys_SysUserInfo model = new Sys_SysUserInfo();
            if (row != null)
            {
                model.ID = row["ID"].ToString();
                model.USERNAME = row["USERNAME"].ToString();
                model.USERPWD = row["USERPWD"].ToString();
                model.USERFULLNAME = row["USERFULLNAME"].ToString();

                model.ROLEID = row["ROLEID"].ToString();

                if (row["USERSTATUS"] != null && row["USERSTATUS"].ToString() != "")
                {
                    model.USERSTATUS = int.Parse(row["USERSTATUS"].ToString());
                }
                model.CREATEUSER = row["CREATEUSER"].ToString();

                if (row["CREATEDATE"] != null && row["CREATEDATE"].ToString() != "")
                {
                    model.CREATEDATE = DateTime.Parse(row["CREATEDATE"].ToString());
                }
                if (row["ISDELETED"] != null && row["ISDELETED"].ToString() != "")
                {
                    model.ISDELETED = int.Parse(row["ISDELETED"].ToString());
                }
                model.REMARKS = row["REMARKS"].ToString();
            }
            return model;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_SysUserInfo GetModel(string userId)
        {
            DataRow row = GetInfoById(userId).Rows[0];
            Sys_SysUserInfo model = new Sys_SysUserInfo();
            if (row != null)
            {
                model.ID = row["ID"].ToString();
                model.USERNAME = row["USERNAME"].ToString();
                model.USERPWD = row["USERPWD"].ToString();
                model.USERFULLNAME = row["USERFULLNAME"].ToString();

                model.ROLEID = row["ROLEID"].ToString();

                if (row["USERSTATUS"] != null && row["USERSTATUS"].ToString() != "")
                {
                    model.USERSTATUS = int.Parse(row["USERSTATUS"].ToString());
                }
                model.CREATEUSER = row["CREATEUSER"].ToString();

                if (row["CREATEDATE"] != null && row["CREATEDATE"].ToString() != "")
                {
                    model.CREATEDATE = DateTime.Parse(row["CREATEDATE"].ToString());
                }
                if (row["ISDELETED"] != null && row["ISDELETED"].ToString() != "")
                {
                    model.ISDELETED = int.Parse(row["ISDELETED"].ToString());
                }
                model.REMARKS = row["REMARKS"].ToString();
            }
            return model;
        }
        #endregion

        #region 用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="PerUserName"></param>
        /// <param name="PerUserPewd"></param>
        /// <returns></returns>
        public DataTable Login(string PerUserName, string PerUserPewd)
        {
            OracleParameter[] parameters = {
                                            new OracleParameter("PerUserName", OracleType.VarChar,20),
                                            new OracleParameter("PerUserPewd", OracleType.VarChar,100),
                                            new OracleParameter("cur_arg", OracleType.Cursor)
                                         };
            parameters[0].Value = PerUserName;
            parameters[1].Value = PerUserPewd;
            parameters[2].Direction = ParameterDirection.Output;

            return DbHelperSQL.FillDataTable(CommandType.StoredProcedure, "P_Sys_UserLogin", parameters);
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="PerUserName">用户名</param>
        /// <param name="PerUserPewd">新密码</param>
        /// <returns></returns>
        public int UpdatePassword(string PerUserName, string PerUserPewd)
        {
            try
            {
                OracleParameter[] parameters = {
				                        	new OracleParameter("PerUserName", OracleType.VarChar,20),
                                            new OracleParameter("PerUserPewd", OracleType.VarChar,100),
                                            new OracleParameter("vout", OracleType.Number)
                                         };
                parameters[0].Value = PerUserName;
                parameters[1].Value = PerUserPewd;
                parameters[2].Direction = ParameterDirection.Output;
                object obj = DbHelperSQL.RunProcedure("P_Sys_UserUpdatePwd", parameters, parameters[2]);
                return Convert.ToInt32(obj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
